package com.matt.system.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.matt.system.dao.SystemMenuDao;
import com.matt.system.domain.TSysmenu;

@Repository
public class SystemMenuDaoImpl extends HibernateDaoSupport implements SystemMenuDao {
	
	@SuppressWarnings("unchecked")
	public List<TSysmenu> findAll(){
		return this.getHibernateTemplate().find("from TSysmenu t order by t.TSmOrder");
	}

	@Override
	public void delete(TSysmenu sysmenu) {
		this.getHibernateTemplate().delete(sysmenu);
	}
	
	@Override
	public void update(TSysmenu sysmenu) {
		this.getHibernateTemplate().update(sysmenu);
	}
	
	@Override
	public void save(TSysmenu sysmenu) {
		this.getHibernateTemplate().save(sysmenu);
	}

	@Override
	public TSysmenu findById(final String id) {
		TSysmenu sysmenu = this.getHibernateTemplate().execute(new HibernateCallback<TSysmenu>() {
			@Override
			public TSysmenu doInHibernate(Session arg0) throws HibernateException,
					SQLException {
				Query query = arg0.createQuery("from TSysmenu t where t.TSmId =?");
				query.setString(0, id);
				return (TSysmenu) query.uniqueResult();
			}
		});
		return sysmenu;
	}

	@Override
	public TSysmenu findByParentId(final String pid) {
		TSysmenu sysmenu = this.getHibernateTemplate().execute(new HibernateCallback<TSysmenu>() {
			@Override
			public TSysmenu doInHibernate(Session arg0) throws HibernateException,
					SQLException {
				Query query = arg0.createQuery("from TSysmenu t where t.TSmParentmenu =?");
				query.setString(0, pid);
				return (TSysmenu) query.uniqueResult();
			}
		});
		return sysmenu;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TSysmenu> findPageByCriteria(final int pageNow,
			final int pageSize, final DetachedCriteria detachedCriteria) {
		List<TSysmenu> list = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = detachedCriteria
								.getExecutableCriteria(arg0);
						criteria.setProjection(null);
						criteria.setFirstResult(pageNow);
						criteria.setMaxResults(pageSize);
						return criteria.list();
					}
				});
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public long getCountByCriteria(final DetachedCriteria detachedCriteria) {
		Long count = this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				Criteria criteria = detachedCriteria.getExecutableCriteria(arg0);
				criteria.setProjection(null);
				return criteria.setProjection(Projections.rowCount()).uniqueResult();
			}
		});
		return count;
	}

	

	@Override
	public long getRows() {
		// TODO 没实现
		return 0;
	}

}
